<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/28
 * Time: 16:28
 */

namespace app\admin\controller\log;


use app\admin\logic\MoneyLogic;
use app\common\controller\Backend;
use app\common\utils\Constant;
use app\common\utils\WeChatRedUtils;
use app\common\utils\WeChatSendRedPackUtils;
use app\common\utils\WeChatSendRedUtils;
use app\common\utils\WeChatTransfersUtils;
use app\index\logic\MessageLogic;
use think\Db;
use think\Exception;

/**
 * 提现记录管理
 * Class WithdrawLog
 * @package app\admin\controller\log
 */
class WithdrawLog extends Backend
{
    protected $model = null;

    public function _initialize(){
        parent::_initialize();
        $this->model = model('WithdrawLog');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key=>&$value){
                $value['user_nickname'] = model('User')->where(array('id'=>$value['user_id']))->value('nickname');
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 审核申请记录
     * @param string $ids
     * @return mixed
     */
    public function verify($ids=''){
        if ($this->request->isAjax()) {
            Db::startTrans();
            try {
                $verify_log = $this->model->where(array('id' => $ids))->find();
                if ($verify_log['status'] != Constant::VERIFY_STATUS_WAIT) {
                    $this->error('该记录已经审核过，无法重复审核!');
                }
                $data = $this->request->post("row/a");
                if(!isset($data['status']) || $data['status'] == ''){
                    $this->error('请选择是否通过审核!');
                }
                if($data['status'] == Constant::VERIFY_STATUS_FAIL){
                    //退还余额
                    $change_type = Constant::CHANGE_TYPE_WITHDRAWAL_FAILURE;
                    $money_type = Constant::MONEY_TYPE_BALANCE;
                    $in_out_type = Constant::IN_OUT_TYPE_IN;
                    $money = $verify_log['money'] + $verify_log['poundage'];
                    $desc = '提现失败，资金返回到余额';
                    $map['id'] = $verify_log['user_id'];
                    model('User')->where($map)->setInc('balance',$money);
                    $current_money = model('User')->where($map)->value('balance');
                    $logic = new MoneyLogic();
                    $logic->saveMoneyLog($money,$current_money,$change_type,
                        $in_out_type,$desc,$verify_log['user_id'],$money_type,0);
                }else{
                    //审核通过 发送红包
                    $utils = new WeChatTransfersUtils();
                    $open_id = model('User')->where(array('id'=>$verify_log['user_id']))->value('open_id');
                    $money = $verify_log['money'] * 100;//单位分
                    $remark = '公众号转账';
                    $result = $utils->transfers($open_id,$money,$remark);
                    if(!$result){
                        Db::rollback();
                        $this->error('微信转账失败!');
                    }
                }
                $this->model->where(['id' => $ids])->update($data);
                Db::commit();
                //发送通知
                $message_logic = new MessageLogic();
                if($data['status'] == Constant::VERIFY_STATUS_FAIL){
                    $message_type = Constant::CONFIG_NAME_WITHDRAWAL_FAILURE_NOTICE;
                }else{
                    $message_type = Constant::CONFIG_NAME_WITHDRAWAL_SUCCESS_NOTICE;
                }
                $message_logic->sendMessage($verify_log['user_id'],$message_type);
                $this->success('审核成功！');
            }catch (Exception $e){
                Db::rollback();
                $this->error('审核失败!','',$e->getMessage());
            }
        }
        return $this->view->fetch();
    }

    private function getConfigByName($name){
        $value = model('Config')->where(array('name'=>$name))->value('value');
        return $value;
    }

    public function test(){
        //审核通过 发送红包
        $utils = new WeChatTransfersUtils();
        $open_id = model('User')->where(array('id'=>19))->value('open_id');
        $money = 1*100;
        $remark = '公众号转账';
        $result = $utils->transfers($open_id,$money,$remark);
        var_dump($result);
        var_dump($open_id);
    }

}